package com.dao.apply;

import com.dao.BaseDao;
import com.mysql.cj.util.StringUtils;
import com.pojo.Apply;
import com.pojo.Good;
import com.pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ApplyDaoImpl implements ApplyDao{

    @Override
    public List<Object> getSearchApplyList(Connection connection, String applyName) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Object> applys = new ArrayList<>();
        List<Object> list = new ArrayList<>();

        if(connection != null){
            StringBuffer sql = new StringBuffer();
            sql.append("select * from user,apply WHERE user.userId=apply.userId");
            if(!StringUtils.isNullOrEmpty(applyName)){
                sql.append(" and  applyName like ?");
                list.add("%"+applyName+"%");
            }


            Object[] o = list.toArray();
            System.out.println("sql : "+sql.toString());
            resultSet = BaseDao.excute(connection,preparedStatement,resultSet,sql.toString(),o);
            while (resultSet.next()){
                Apply apply = new Apply();
                apply.setApplyId(resultSet.getInt("applyId"));
                apply.setUserId(resultSet.getInt("userId"));
                apply.setApplyName(resultSet.getString("applyName"));
                apply.setApplyAddress(resultSet.getString("applyAddress"));
                apply.setApplyExplain(resultSet.getString("applyExplain"));
                User user = new User();
                user.setUserName(resultSet.getString("userName"));
                user.setUserQq(resultSet.getString("userQQ"));
                user.setUserAddress(resultSet.getString("userAddress"));
                user.setUserPhone(resultSet.getString("userPhone"));
                apply.setUser(user);
                applys.add(apply);
            }

            System.out.println("Dao中："+applys.toString());
            BaseDao.closeResource(null,preparedStatement,resultSet);
        }
        return applys;
    }

    @Override
    public List<Object> getUserApplyList(Connection connection, int userId) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Object> applys = new ArrayList<>();
        String sql = "select * from apply where userId = ?";
        Object[] o = {userId};
        resultSet = BaseDao.excute(connection, preparedStatement, resultSet, sql, o);
        while (resultSet.next()) {
            Apply apply = new Apply();
            apply.setApplyId(resultSet.getInt("applyId"));
            apply.setUserId(resultSet.getInt("userId"));
            apply.setApplyName(resultSet.getString("applyName"));
            apply.setApplyExplain(resultSet.getString("applyExplain"));
            apply.setApplyAddress(resultSet.getString("applyAddress"));
            applys.add(apply);
        }

        System.out.println(applys.toString());
        BaseDao.closeResource(null, preparedStatement, resultSet);
        return applys;
    }


    /*删除用户拾取的物品*/
    @Override
    public int UserApplyListDel(Connection connection, int applyId) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        String sql = "delete from apply where applyId = ?";
        Object []o = {applyId};
        int flag = BaseDao.excute(connection,preparedStatement,sql,o);
        System.out.println("apply del中flag为："+flag);
        return flag;
    }

    @Override
    public int ApplyAdd(Connection connection, int userId, String applyName, String applyAddress, String applyExplain) throws SQLException {
        int flag = 0;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "insert into apply(userId,applyName,applyAddress,applyExplain) values(?,?,?,?)";
        Object []o = {userId,applyName,applyAddress,applyExplain};
        if(connection != null){
            flag = BaseDao.excute(connection,preparedStatement,sql,o);
        }
        BaseDao.closeResource(null,null,null);
        return flag;
    }





}
